package com.wingsfrog.puzzle.service.impl;

import com.wingsfrog.puzzle.modules.dao.ScoreDao;
import com.wingsfrog.puzzle.modules.entity.Score;
import com.wingsfrog.puzzle.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    ScoreDao dao;

    @Override
    public Integer getBestScore(Integer userId, Integer gameId, Integer score) {
        Integer lastScore = dao.getScore(userId, gameId);
        if (lastScore == null) {
            Score s = new Score();
            s.setUserId(userId);
            s.setGameId(gameId);
            s.setScore(score);
            dao.save(s);
            return score;
        } else if (lastScore < score) {
            dao.updateScore(userId, gameId, score);
            return score;
        } else
            return lastScore;
    }

}
